﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgileTimeTracker.Client.Wpf.Common;
using AgileTimeTracker.Client.Wpf.Services;
using AgileTimeTracker.Domain.Exceptions;
using AgileTimeTracker.Domain.UserModule;

namespace AgileTimeTracker.Client.Wpf.AccessControlModule
{
    public class EditProjectViewModel : PageViewModel
    {
        private UserProject userProject;

        private string login;
        private string name;
        private string url;
        private string domain;        

        public EditProjectViewModel()
        {
            this.Title = "Create New Project";
        }

        public EditProjectViewModel(UserProject userProject)
            : this()
        {
            this.userProject = userProject;
            this.Title = "Edit Project";
            if (this.userProject != null)
            {
                this.Name = this.userProject.Name;
                this.Url = this.userProject.Url;
                this.Login = this.userProject.Login;
                this.Domain = this.userProject.Domain;
            }
        }

        public Func<string> GetPassword { get; set; }

        public string Login
        {
            get { return this.login; }
            set { this.OnPropertyChanged(ref this.login, value); }
        }

        public string Name
        {
            get { return this.name; }
            set { this.OnPropertyChanged(ref this.name, value); }
        }

        public string Url
        {
            get { return this.url; }
            set { this.OnPropertyChanged(ref this.url, value); }
        }

        public string Domain
        {
            get { return this.domain; }
            set { this.OnPropertyChanged(ref this.domain, value); }
        }

        #region Commands

        public RelayCommand SaveCommand { get; set; }                

        #endregion // Commands

        protected override void InititalizeCommands()
        {
            base.InititalizeCommands();
            this.SaveCommand = new RelayCommand(this.Save);
        }

        protected override void Close()
        {
            MainWindowViewModel.Instance.CurrentView = new ProjectSelectionViewModel();        
        }

        private void Save()
        {
            var password = this.GetPassword();
            var errors = new List<ValidationError>();
            errors.CheckRequired(this.Name, "name", "name");
            errors.CheckRequired(this.Login, "login", "login");
            errors.CheckRequired(this.Url, "url", "url");
            errors.CheckRequired(this.Domain, "domain", "domain");
            errors.CheckRequired(password, "password", "password");

            if (errors.IsValidOrThrow())
            {
                if (this.userProject == null)
                {
                    this.userProject = new UserProject();
                    MainWindowViewModel.Instance.CurrentUser.Projects.Add(this.userProject);
                }

                this.userProject.Login = this.login;
                this.userProject.Name = this.name;
                this.userProject.Password = password;
                this.userProject.Url = this.url;
                this.userProject.Domain = this.domain;
                AppService.Instance.SaveUser(MainWindowViewModel.Instance.CurrentUser);
                this.Close();
            }
        }        
    }
}
